<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /*
            这题其实类似于接雨水，暴力法甚至更简单。
            遍历heights[i]每一根柱子，找到它左右两边大于或等于它的柱子。然后每次求和即可
            但是hard就难在超出时间限制了...
            */
            var largestRectangleArea = function (heights) {
                let result = 0
                for (let i = 0; i < heights.length; i++) {
                    let left = i - 1
                    let right = i + 1
                    while (heights[left] && heights[left] >= heights[i]) {
                        left--
                    }
                    while (heights[right] && heights[right] >= heights[i]) {
                        right++
                    }
                    //计算面积
                    result = Math.max(result, heights[i] * (right - left - 1))
                }
                return result
            }
        </script>
    </body>
</html>
